{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e8d1c0d-9253-443e-9373-db7597275fd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pyspark的版本要和服务端对应\n",
    "!pip install pyspark==3.4.1 --index-url https://mirrors.aliyun.com/pypi/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3cc3986d-3396-4bc6-b2bd-b34fef2fc761",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "from random import random\n",
    "from operator import add\n",
    "import sys\n",
    "from pyspark.sql import SparkSession\n",
    "from pyspark.sql.functions import rand\n",
    "from pyspark.sql import SparkSession\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "id": "6a7411930c2d81e"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "33e2a48d-501c-4eb2-b6d0-7ed04dbe70a9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 创建 SparkSession\n",
    "spark = SparkSession.builder \\\n",
    "    .appName(\"PythonPi\") \\\n",
    "    .master('spark://myspark-master-0.myspark-headless.kubeflow.svc.cluster.local:7077') \\\n",
    "    .config(\"spark.executor.memory\", \"2g\") \\\n",
    "    .config(\"spark.executor.cores\", \"2\") \\\n",
    "    .config(\"spark.cores.max\", \"8\") \\\n",
    "    .config(\"spark.driver.memory\", \"2g\") \\\n",
    "    .config(\"spark.ui.enabled\", False) \\\n",
    "    .config(\"spark.driver.port\", os.getenv('PORT1')) \\\n",
    "    .config(\"spark.blockManager.port\", os.getenv('PORT2')) \\\n",
    "    .config(\"spark.driver.bindAddress\", '0.0.0.0') \\\n",
    "    .config(\"spark.driver.host\", os.getenv('SERVICE_EXTERNAL_IP')) \\\n",
    "    .getOrCreate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e0c4272a-6ea6-4ee3-b184-017075d9e83b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Stage 0:>                                                          (0 + 2) / 2]\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  summary                  rand\n",
      "0   count               1000000\n",
      "1    mean    0.4998700608314018\n",
      "2  stddev   0.28877290360565244\n",
      "3     min  4.200237760931813E-7\n",
      "4     25%   0.24931865816196142\n",
      "5     50%      0.49956198713249\n",
      "6     75%    0.7501200528782145\n",
      "7     max    0.9999984014664925\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                \r"
     ]
    }
   ],
   "source": [
    "# 生成随机数 DataFrame\n",
    "df = spark.range(0, 1000000).withColumn(\"rand\", rand(seed=42))\n",
    "\n",
    "# 分析随机数\n",
    "summary = df.select(\"rand\").summary().toPandas()\n",
    "print(summary)\n",
    "\n",
    "# 将结果转换为 Pandas DataFrame\n",
    "summary_df = pd.DataFrame(summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "92f59c33-5bec-4cfa-8533-a9f05afe58d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 关闭 SparkSession\n",
    "spark.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "311995eb-0ca2-48a3-8e01-ba3e4af3c11f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
